﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Application.SiteLibraryManage;
using YunSpace.Domain.Entity.SiteLibraryManage;
using YunSpace.Core;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
#endregion

namespace YunSpace.Web.Areas.SiteManage.Controllers
{
    public class PhotosController : ControllerBase
    {
        private PhotosApp  photosApp = new PhotosApp();
        static string basePath = "http://storage.yunspace.com.cn/";

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult Photos()
        {
            return View();
        }


        // <summary>
        /// 获取数据集合
        /// </summary>
        /// <param name="keyValue">查询条件</param>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetPhotosListJson(Pagination pagination, string queryJson)
        {

            var data = new
            {
                rows = photosApp.GetList(pagination, queryJson),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };

            return Content(data.ToJson());
        }


        // <summary>
        /// 获取数据集合
        /// </summary>
        /// <param name="queryJson">查询条件</param>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetPhotosList(string queryJson)
        {
            var data = photosApp.GetList(queryJson);
            return Content(data.ToJson());
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = photosApp.GetForm(keyValue);
            return Content(data.ToJson());
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UploadPathImage()
        {
            try
            {   
                var file = Request.Files[0];
                string Extension = Path.GetExtension(file.FileName); //获取扩展名
                Extension = DateTime.Now.ToString("yyyyMMddhhmmss") + Extension;
                var saveKey = string.Format("uploads/photo/path/{0}/{1}", 1, Extension);

                int FileLen = file.ContentLength;
                byte[] input = new byte[FileLen];


                using (var UpLoadStream = file.InputStream)
                {
                    UpLoadStream.Read(input, 0, FileLen);
                    UpLoadStream.Position = 0;
                    var result = QiniuHelper.Instance.HttpUploadStream(UpLoadStream, saveKey);
                    if (result.Code == 200)
                    {
                        return Json(new { data= basePath + saveKey,path= saveKey, state=1,msg="成功" });
                    }
                    return Json(new { state=1,msg="上传失败" });
                }
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    msg = ex.Message
                });
            }
        }

       
        /// <summary>
        /// 提交表单
        /// </summary>
        /// <param name="photosEntity">表单实体</param>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(PhotosEntity photosEntity, string keyValue)
        {
            photosApp.SubmitForm(photosEntity, keyValue);

            return Success("操作成功。");
        }

        /// <summary>
        /// 删除表单
        /// </summary>
        /// <param name="keyValue">表单主键</param>
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            photosApp.DeleteForm(keyValue);

            return Success("删除成功。");
        }





    }
}


